package com.songqintuan.platform.tree;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.Company;
import com.songqintuan.model.po.Organization;
import com.songqintuan.persist.service.hibernate.ICompanyService;
import com.songqintuan.persist.service.hibernate.IOrganizationService;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;

public class OrganizationDTree {

	private Map<String, Long> pids = new HashMap<String, Long>();

	public Map<String, Long> getPids() {
		return pids;
	}

	public void setPids(Map<String, Long> pids) {
		this.pids = pids;
	}

	public OrganizationDTree() {

	}

	public String getAllOrganization() {
		StringBuffer result = new StringBuffer();
		IOrganizationService<Organization, Long> organizationService = (IOrganizationService<Organization, Long>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.ORGANIZATION_SERVICE_BEAN_NAME);
		organizationService.setEntityClass(Organization.class);
		organizationService.setPKClass(Long.class);
		List<Organization> resources = organizationService.findBy("status", 0, "struct", true);
		return createTreeInfo(resources);
	}
	
	/** */
	/**
	 * 　　　　 * @生成创建tree的脚本 　　　　 * @param treeInfoList 　　　　 * @return 　　　　
	 */

	private String createTreeInfo(List<Organization> treeInfoList){
		StringBuffer contents = new StringBuffer();
		ICompanyService<Company, String> companyService = (ICompanyService<Company, String>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.COMPANY_SERVICE_BEAN_NAME);
		companyService.setEntityClass(Company.class);
		companyService.setPKClass(String.class);
		Company company = companyService.getAll().get(0);
		String nodeName = "";
		if (null != company) {
			nodeName = company.getLabel();
		}
		contents.append("<script type=\"text/JavaScript\">\n");
		contents.append("var d = new Dtree(\"d\");\n");
		contents.append("d.add(new Node(0,-1,\"" + nodeName + "\", \"javascript:go('')\",\"\",\"\",\"\",\"\",\"\"));\n");

		Organization treeInfo = null;
		for (int i = 0; i < treeInfoList.size(); i++) {
			treeInfo = (Organization) treeInfoList.get(i);
			contents.append(createOneItem(treeInfo));
		}
		contents.append("document.write(d);\n");
		contents.append("</script>");
		return contents.toString();
	}
	private String createOneItem(Organization treeInfo) {
		StringBuffer result = new StringBuffer();
		result.append("d.add(new Node(");
		//第一个参数表示当前节点的ID
		result.append(treeInfo.getId() + ",");
		//第二个参数表示当前节点的父节点的ID,根节点的值为 -1
		result.append(treeInfo.getPid() + ",");
		//第三个参数节点要显示的文字
		result.append("\"" + treeInfo.getLabel() + "\",");
		//第四个参数节点的Url
		result.append("\"" + "javascript:executeOrgClickEvent('" + treeInfo.getId() + "')" + "\",");
		//第五个参数鼠标移至该节点时节点的Title
		result.append("\"" + treeInfo.getLabel() + "\",");
		//第六个参数节点的target
		result.append("\"\",");
		//第七个参数用做节点的图标,节点没有指定图标时使用默认值
		result.append("\"\",");
		//第八个参数用做节点打开的图标,节点没有指定图标时使用默认值
		result.append("\"\",");
		//第九个参数判断节点是否打开
		result.append(false);
		result.append("));\n");
		return result.toString();
	}
}
